﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Manager_System
{
    public partial class add_qualitymanager : Form
    {
    
        string t = "";
        string Id = "";
        string Interid = "";
        main_form form;
        public add_qualitymanager(main_form form1)
        {
            t = "添加";
            InitializeComponent();
            note_tb.MaxLength = 200;

            form = form1;
        }
        public add_qualitymanager(string interid,string type, string id, string potnum, string material, string weight, string note, main_form form1)
        {
            InitializeComponent();
            if (type.Equals("钢板"))
            { this.type_cb.SelectedIndex = 1; }
            else
            { this.type_cb.SelectedIndex = 0; }
             
             Interid = interid;
            t = "修改";
            note_tb.Text = note;
            Id = id.Substring(0, 4) + "/" + id.Substring(4, 2) + "/" + id.Substring(6, 2);
            time_dt.Value = Convert.ToDateTime(Id);
            form = form1;
            this.material_tb.Text = material;
            weight_tb.Text = weight;
            potnum_tb.Text = potnum;
            note_tb.Text = note;
     
        }
        public void add_qualitymanager_Load(object sender, EventArgs e)
        {
            time_dt.Format = DateTimePickerFormat.Custom; //设置为显示格式为自定义
            time_dt.CustomFormat = "yyyy-MM-dd";
        }

        private void comfirm_bt_Click(object sender, EventArgs e)
        {
            string ID = time_dt.Text.Substring(0, 4) + time_dt.Text.Substring(5, 2) + time_dt.Text.Substring(8, 2);
            string sql = "";
            if (t.Equals("添加"))
            {
                sql = "insert into T_Quality (ID,Note,Type,Material,Weight,Potnum) values('" + ID + "','" + note_tb.Text + "','" + type_cb.SelectedItem.ToString() + "','" + material_tb.Text + "','"+weight_tb.Text+"','"+potnum_tb.Text+"')";
            }
            else if (t.Equals("修改"))
            {
                sql = "update T_Quality set ID='" + ID + "',Note='" + note_tb.Text + "',Type='" + type_cb.SelectedItem.ToString() + "',Material='" + material_tb.Text + "',Weight='" + weight_tb.Text + "',Potnum='" + potnum_tb.Text + "' where InterID='" + Interid + "'";
            }
            if (note_tb.Text.Trim() != String.Empty && material_tb.Text.Trim() != String.Empty && weight_tb.Text.Trim() != String.Empty && potnum_tb.Text.Trim() != String.Empty)
            {

                DBhelp db = new DBhelp();
                int rs = db.update(sql);
                if (rs == 1)
                {
                    MessageBox.Show(t.ToString() + "成功！");
                }
                else
                {
                    MessageBox.Show(t.ToString() + "失败！");
                }
                db.close();

                form.Refresh_Quality_LV();
            }
            else
            {
                MessageBox.Show("请输入相关数值！");
            }
        }

        private void note_tb_TextChanged(object sender, EventArgs e)
        {
            note_size.Text = "您还可以输入" + (100 - note_tb.Text.Length).ToString() + "个汉字";
        }
    }
}
